package com.xcity.game.equip.suit4.impl;

import java.util.List;

import com.xcity.game.combat.Action;
import com.xcity.game.combat.ActionEffect;
import com.xcity.game.combat.ActionEffect.Value;
import com.xcity.game.combat.Combat;
import com.xcity.game.combat.CombatTeam;
import com.xcity.game.combat.Round;
import com.xcity.game.combat.attr.CombatAttributeType;
import com.xcity.game.combat.buff.CombatBuff;
import com.xcity.game.combat.buff.CombatBuffTemplate;
import com.xcity.game.combat.unit.CombatUnit;
import com.xcity.game.common.GameStaticConfig;
import com.xcity.game.equip.suit4.BaseSuit4;
import com.xcity.game.equip.template.EquipSuitEffectTemplate;

/**
 * 蔻驰4件套<br/>
 * 回合结束时{50}%几率给全体队友增加一个个吸收等同于{15}%生命上限的护盾<br/>
 * @author yang.li
 *
 */
public class Suit4Coach extends BaseSuit4 {

	public Suit4Coach(CombatUnit unit, EquipSuitEffectTemplate template) {
		super(unit, template);
	}

	@Override
	public void roundEnd(Round round) {
		if (!round.ref().isEnd() && Combat.RND.nextInt(GameStaticConfig.percentBase) < template.getSuit4Param(0)) {
			CombatBuffTemplate buffTemplate = CombatBuffTemplate.find(3);
			CombatTeam team = unit.getTeam();
			List<CombatUnit> units = team.all();
			Action act = null;
			for (int i = 0; i < units.size(); i++) {
				CombatUnit u = units.get(i);
				if (!u.isDied() && u.getId() != unit.getId()) {
					int hp = (int) (u.getMaxHp() * template.getSuit4Param(1) / GameStaticConfig.percentBase);
					if (hp > 0) {
						if (act == null) {
							act = new Action(unit);
							round.add(act);
						}
						ActionEffect eff = act.addTarget(u);
//						eff.effect(type, delta);
						act.addBuff(new BUFF(buffTemplate, u, hp));
					}
				}
			}
		}
	}
	
	static class BUFF extends CombatBuff {
		int hp;
		public BUFF(CombatBuffTemplate template, CombatUnit owner, int hp) {
			super(template, owner);
			this.hp = hp;
		}
		@Override
		public void preDamage(ActionEffect effect) {
			active = true;
			int delta = effect.getHpDelta();
			if (delta < 0 && hp > 0) {
				hp += delta;
				Value v = effect.getValue(CombatAttributeType.HP);
				v.setDelta(hp > 0 ? 0 : hp);
			}
		}
		@Override
		public void roundEnd(Round round) {
//			super.roundEnd(round);
			if (state != STATE_REMOVED && hp <= 0) { // 破盾
				uneffect();
				round.addBuff(this);
			}
		}
	}

}
